package com.shsoft.component;

import com.shsoft.config.PCStatusConstant;
import com.shsoft.util.FileUtil;
import javafx.geometry.Bounds;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;

public class TextComponent extends BaseComponent{

    private String content;

    private boolean stroke;

    private String fontName;

    private int fontSize;

    private String alignment;

    private double fillColorR,fillColorG,fillColorB,fillColorA;

    private double strokeColorR,strokeColorG,strokeColorB,strokeColorA;

    private double borderX,borderY;

    private double borderWidth,borderHeight;

    @Override
    public void drawComponent(GraphicsContext gc) {
        gc.setFont(FileUtil.getFont(fontName, fontSize));
        if(alignment.equals(PCStatusConstant.ALIGNMENT_LEFT)){
            gc.setTextAlign(TextAlignment.LEFT);
        }else if(alignment.equals(PCStatusConstant.ALIGNMENT_CENTER)){
            gc.setTextAlign(TextAlignment.CENTER);
        }else if(alignment.equals(PCStatusConstant.ALIGNMENT_RIGHT)){
            gc.setTextAlign(TextAlignment.RIGHT);
        }
        gc.setFill(getFillColor());
        gc.fillText(content, getX(), getY());
        if(stroke){
            gc.setStroke(getStrokeColor());
            gc.strokeText(content, getX(), getY());
        }
    }

    @Override
    public void drawComponentBorder(Canvas canvas) {
        GraphicsContext gc = canvas.getGraphicsContext2D();
        Text text = new Text();
        text.setFont(FileUtil.getFont(fontName, fontSize));
        text.setText(content);
        Bounds layoutBounds = text.getLayoutBounds();
        borderHeight = layoutBounds.getHeight();
        borderWidth = layoutBounds.getWidth() + 2;
        borderY = getY() - fontSize;
        if(alignment.equals(PCStatusConstant.ALIGNMENT_LEFT)){
            borderX = getX();
        }else if(alignment.equals(PCStatusConstant.ALIGNMENT_CENTER)){
            borderX = getX() - (borderWidth - 2) / 2;
        }else if(alignment.equals(PCStatusConstant.ALIGNMENT_RIGHT)){
            borderX = getX() - borderWidth;
        }
        if(fontName.equals("像素体")){
            borderY = getY() - fontSize + fontSize / 4;
        }else if(fontName.equals("琉璃体") || fontName.equals("万事全兴体")){
            borderY = getY() - fontSize + fontSize / 6;
        }else if(fontName.equals("鸿蒙体") || fontName.equals("风雅黑体") || fontName.equals("GemunuLibre") || fontName.equals("数码数字体")){
            borderY = getY() - fontSize + fontSize / 8;
        }
        gc.setStroke(Color.GREY);
        gc.strokeRect(borderX, borderY, borderWidth, borderHeight);
    }

    @Override
    public boolean isMe(double x, double y) {
        if(x >= borderX && x <= borderX + borderWidth && y >= borderY && y <= borderY + borderHeight){
            return true;
        }
        return false;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Color getFillColor() {
        return new Color(fillColorR,fillColorG,fillColorB,fillColorA);
    }

    public void setFillColor(Color fillColor) {
        this.fillColorR = fillColor.getRed();
        this.fillColorG = fillColor.getGreen();
        this.fillColorB = fillColor.getBlue();
        this.fillColorA = fillColor.getOpacity();
    }

    public Color getStrokeColor() {
        return new Color(strokeColorR,strokeColorG,strokeColorB,strokeColorA);
    }

    public void setStrokeColor(Color strokeColor) {
        this.strokeColorR = strokeColor.getRed();
        this.strokeColorG = strokeColor.getGreen();
        this.strokeColorB = strokeColor.getBlue();
        this.strokeColorA = strokeColor.getOpacity();
    }

    public String getAlignment() {
        return alignment;
    }

    public void setAlignment(String alignment) {
        this.alignment = alignment;
    }

    public boolean isStroke() {
        return stroke;
    }

    public void setStroke(boolean stroke) {
        this.stroke = stroke;
    }

    public int getFontSize() {
        return fontSize;
    }

    public void setFontSize(int fontSize) {
        this.fontSize = fontSize;
    }

    public String getFontName() {
        return fontName;
    }

    public void setFontName(String fontName) {
        this.fontName = fontName;
    }

}
